package com.yyxk.thousandtime.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.yyxk.thousandtime.R;
import com.yyxk.thousandtime.ibase.IBaseView;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 项目名称：ThousandTime
 * 包名:com.yyxk.thousandtime.base
 * 类描述：
 * 创建人：LX
 * 创建时间：2017/6/9 下午1:24
 * 修改人：LX
 * 修改时间：2017/6/9 下午1:24
 * 修改备注：
 */

public abstract class BaseActivity<P extends BasePresenter,T extends ViewDataBinding> extends AppCompatActivity implements IBaseView{
    public static final int NORMAL_MODE=0x11,CUSTOMER=0x22;
    private int mMode=NORMAL_MODE;
    public Toolbar mToolbar;
    public View mRootView;//布局内容
    private View mLoadingLayout,mErrorLayout,mEmptyLayout;
    private ImageView mErrorImage,mEmptyImage;
    private CompositeSubscription mCompositeSubscription;
    public P mPresenter;
    public T mDataBinding;
    public boolean mHideMenu;

    public abstract P initPresenter();

    public abstract @LayoutRes int setLayoutId();

    public abstract void init();

    public void setRootMode(int mode){
        mMode=mode;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewGroup group= (ViewGroup) LayoutInflater.from(this).inflate(R.layout.activity_base_normal,null);
        mToolbar= (Toolbar) group.findViewById(R.id.toolbar);
        mLoadingLayout=group.findViewById(R.id.loading_layout);
        mErrorLayout=group.findViewById(R.id.loading_error_layout);
        mEmptyLayout=group.findViewById(R.id.loading_empty_layout);
        mErrorImage= (ImageView) group.findViewById(R.id.loading_error_image);
        mEmptyImage= (ImageView) group.findViewById(R.id.loading_empty_image);
        mErrorImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();//重新加载
            }
        });
        mEmptyImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();//重新加载
            }
        });
        initToolBar();
        if(mMode==NORMAL_MODE){
            mRootView=LayoutInflater.from(this).inflate(setLayoutId(),null);
            RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            mRootView.setLayoutParams(params);
            RelativeLayout relativeLayout= (RelativeLayout) group.findViewById(R.id.container);
            relativeLayout.addView(mRootView);
            setContentView(group);
            mDataBinding= DataBindingUtil.bind(mRootView);
        }else if(mMode==CUSTOMER){
//            setContentView(setLayoutId());
            mDataBinding=DataBindingUtil.setContentView(this,setLayoutId());
        }
        showContentView();
        mPresenter=initPresenter();
        mPresenter.initPresenter(this);
        init();
    }

    /**
     * 显示主页面
     */
    @Override
    public void showContentView(){
        if(mRootView!=null)
            mRootView.setVisibility(View.VISIBLE);
        mEmptyLayout.setVisibility(View.GONE);
        mErrorLayout.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.GONE);
    }

    /**
     * 显示加载页面
     */
    @Override
    public void showLoading(){
        if(mRootView!=null)
            mRootView.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.GONE);
        mErrorLayout.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.VISIBLE);
    }

    /**
     * 显示错误页面
     */
    @Override
    public void showError(){
        if(mRootView!=null)
            mRootView.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.GONE);
        mErrorLayout.setVisibility(View.VISIBLE);
    }

    /**
     * 显示空界面
     */
    @Override
    public void showEmpty(){
        if(mRootView!=null)
            mRootView.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.VISIBLE);
        mLoadingLayout.setVisibility(View.GONE);
        mErrorLayout.setVisibility(View.GONE);
    }

    /**
     * 重新加载
     */
    public abstract void onRefresh();

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if(mHideMenu) {
            menu.findItem(R.id.action_share).setVisible(false);
            menu.findItem(R.id.action_settings).setVisible(false);
            menu.findItem(R.id.action_me).setVisible(false);
            invalidateOptionsMenu();
        }
        return super.onPrepareOptionsMenu(menu);
    }


    private void initToolBar() {
        setSupportActionBar(mToolbar);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.action_me:
                        break;
                    case R.id.action_share:
                        break;
                    case R.id.action_settings:
                        break;
                }
                return true;
            }
        });
    }

    /**
     * 添加到Subscription队列
     * @param s
     */
    public void addSubscription(Subscription s) {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = new CompositeSubscription();
        }
        this.mCompositeSubscription.add(s);
    }

    /**
     * 统一卸载Subscribe
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (this.mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            this.mCompositeSubscription.unsubscribe();
        }
    }

    /**
     * 卸载Subscribe
     */
    public void removeSubscription() {
        if (this.mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            this.mCompositeSubscription.unsubscribe();
        }
    }
}
